#include <REGX52.H>

typedef unsigned char  u8;
#define SCL P1^7;
#define SDA P1^6;

void IIC_Start()//起始信号
{
    SCL = 1;
    SDA = 1;
    SDA = 0;
    SCL = 0;
}

void IIC_Sendbyte(u8 byte)//发送从设备地址
{
    u8 i;
    for(i = 0; i < 8; i++)
    {
        SDA = (byte & (0x80 >> i)) == 0 ? 0 : 1;
        SCL = 1;
        SCL = 0;
    }
}

bit IIC_ReceiveAck()//接收确认位
{
    bit ack;
    SDA = 1;
    SCL = 1;
    ack = SDA;
    SCL = 0;
    return ack;
}

u8 IIC_Receivebyte()//接受一个字节
{
    u8 byte = 0;
    u8 i;
    for(i = 0; i < 8; i++)
    {
        SCL = 1;
        byte = (byte << 1) | SDA;
        SCL = 0;
    }
    return byte;
}

void IIC_SendAck(bit ack)//发送确认信号
{
    SDA = ack;
    SCL = 1;
    SCL = 0;
}

void IIC_Stop()//结束信号
{
    SDA = 0;
    SCL = 1;
		SDA = 1;
}

